package com.cloudkinto.service.stock.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.ProductStockFlowingDao;
import com.cloudkinto.dto.stock.LeftQtyDto;
import com.cloudkinto.dto.stock.flowing.FlowingPageDto;
import com.cloudkinto.entity.ProductStockFlowingDo;
import com.cloudkinto.enumType.stock.StockOrderTypeEnum;
import com.cloudkinto.service.company.CompanyService;
import com.cloudkinto.service.stock.ProductStockFlowingService;
import com.cloudkinto.service.stock.exportVo.*;
import com.cloudkinto.service.warehouse.WarehouseService;
import com.cloudkinto.vo.stock.ProductFlowingInEt;
import com.cloudkinto.vo.stock.ProductFlowingOutEt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存流水 service实现类
 * </p>
 *
 * @author hua
 * @since 2024-11-28
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ProductStockFlowingServiceImpl extends ServiceImpl<ProductStockFlowingDao, ProductStockFlowingDo> implements ProductStockFlowingService {
    @Autowired
    private ProductStockFlowingDao dao;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private WarehouseService warehouseService;

    @Override
    public void insertBatch(List<ProductStockFlowingDo> list) {
        if (list.isEmpty()) {
            return;
        }
        dao.insertBatch(list);
    }

    @Override
    public SingleResult pageInit(Long userId, Long companyId, Long tenantId, String source) {
        Map<String, Object> result = new HashMap<>();

        if ("oms".equals(source)) {
            result.put("warehouseList", warehouseService.getWarehouseCompany(companyId, tenantId));
        } else if ("omp".equals(source)) {
            result.put("warehouseList", warehouseService.getList(tenantId));
            result.put("companyList", companyService.getCompanyList(userId, tenantId, source));
        } else if ("wms".equals(source)) {
            result.put("companyList", companyService.getCompanyList(userId, tenantId, source));
        }
        result.put("orderTypeList", StockOrderTypeEnum.getStockOrderTypeEnum());
        return SingleResult.success(result);
    }

    @Override
    public List<FlowingPageDto> getList(Map<String, Object> map) {
        return dao.list(map);
    }

    @Override
    public PageResult getListPage(Map<String, Object> map) {
        IPage<FlowingPageDto> page = pageInit(map);
        page = dao.list(page, map);
        PageResult result = new PageResult(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    private IPage<FlowingPageDto> pageInit(Map<String, Object> map) {
        IPage<FlowingPageDto> page = new Page<>();
        page.setSize(map.get("pageSize") != null ? Long.parseLong(map.get("pageSize").toString()) : 10);
        page.setCurrent(map.get("current") != null ? Long.parseLong(map.get("current").toString()) : 1);
        return page;
    }

    /**
     * 产品库存入库
     *
     * @param et
     */
    public void insertProForIn(ProductFlowingInEt et) {
        //入库时 剩余批次库存 = 今天入库剩余库存
        //入库时 批次号= BNyyyyMMdd
        String batchStr = DateUtils.dateFormatToString(new Date(), "yyyyMMdd");
        LeftQtyDto closedQtyDto = dao.getProductClosedQty(batchStr, et.getOrderType(), et.getType(), et.getSku(), et.getStockAttribute(), et.getWarehouseId(), et.getCompanyId(), et.getTenantId());
        ProductStockFlowingDo flowingDo = BeanConvert.INSTANCE.productStockFlowingDo(et);
        if (closedQtyDto != null) {
            flowingDo.setCloseBatchQty(closedQtyDto.getCloseBatchQty());
            flowingDo.setCloseQty(closedQtyDto.getClosedQty());
            flowingDo.setCloseGoodQty(closedQtyDto.getClosedGoodQty());
        }
        //批次号
        flowingDo.setInnerBatchNo("BN" + batchStr);
        dao.insert(flowingDo);
    }

    /**
     * 产品库存出库
     *
     * @param et
     */
    public void insertProForOut(ProductFlowingOutEt et) {
        //出库时 剩余批次库存 = 今天入库剩余库存
        //出库时 批次号 需要传进来
        LeftQtyDto closedQtyDto = dao.getProductClosedQty(et.getInnerBatchNo(), et.getSourceOrderType(), et.getType(), et.getSku(), et.getStockAttribute(), et.getWarehouseId(), et.getCompanyId(), et.getTenantId());
        ProductStockFlowingDo flowingDo = BeanConvert.INSTANCE.productStockFlowingDo(et);
        if (closedQtyDto != null) {
            flowingDo.setCloseBatchQty(closedQtyDto.getCloseBatchQty());
            flowingDo.setCloseQty(closedQtyDto.getClosedQty());
            flowingDo.setCloseGoodQty(closedQtyDto.getClosedGoodQty());
        }
        flowingDo.setInnerBatchNo("BN" + et.getInnerBatchNo());
        dao.insert(flowingDo);
    }



    @Override
    public void productExportFlowingOms(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<ProductFlowingOmsExportVo> exportVos = BeanConvert.INSTANCE.productFlowingOmsExportVo(list);
        String fileName = "产品库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, ProductFlowingOmsExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void boxExportFlowingOms(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<BoxFlowingOmsExportVo> exportVos = BeanConvert.INSTANCE.boxFlowingOmsExportVo(list);
        String fileName = "箱库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, BoxFlowingOmsExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void productExportFlowingOmp(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<ProductFlowingOmpExportVo> exportVos = BeanConvert.INSTANCE.productFlowingOmpExportVo(list);
        String fileName = "产品库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, ProductFlowingOmpExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    @Override
    public void boxExportFlowingOmp(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<BoxFlowingOmpExportVo> exportVos = BeanConvert.INSTANCE.boxFlowingOmpExportVo(list);
        String fileName = "箱库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, BoxFlowingOmpExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }





    @Override
    public void productExportFlowingWms(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<ProductFlowingWmsExportVo> exportVos = BeanConvert.INSTANCE.productFlowingWmsExportVo(list);
        String fileName = "产品库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, ProductFlowingWmsExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    @Override
    public void boxExportFlowingWms(Map<String, Object> map, HttpServletResponse response) {
        List<FlowingPageDto> list = dao.list(map);
        List<BoxFlowingWmsExportVo> exportVos = BeanConvert.INSTANCE.boxFlowingWmsExportVo(list);
        String fileName = "箱库存流水" + TimeUtils.getCurrentDateFormat("MMddHHmmss");
        try {
            ExcelUtils.exportExcel(exportVos, BoxFlowingWmsExportVo.class, fileName, new ExportParams(), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



}
